<?php

/**
 * Description of down
 *
 * @author krok
 */

header('Content-type:text/plain; charset=utf-8;');

ignore_user_abort(true);
ini_set('max_execution_time', '0');

class download
{
    static public $nl			= "\r\n";
    static public $download_dir	= './down/';

    static public function loading($url)
    {
	$sigment = parse_url($url);

	$stream	    =	fsockopen($sigment['host'], 80, $errno, $errstr, 30) or die($errno.' : '.$errstr);
	$request    =	'GET '.$sigment['path'].$sigment['query'].' HTTP/1.1'.self::$nl;
	$request    .=	'Host: '.$sigment['host'].self::$nl;
	$request    .=	'Accept: */*'.self::$nl;
	$request    .=	'User-Agent: Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20100101 Firefox/7.0.1'.self::$nl;
	$request    .=	'Connection: Close'.self::$nl.self::$nl;

	fwrite($stream, $request);

	do
            $line = fgets($stream, 8192);
	while(strspn($line, self::$nl) !== strlen($line));

	$output = fopen(self::$download_dir.md5($url).'.output', wb);

	while( ! feof($stream))
		fwrite($output, fread($stream, 1024 * 8));

	fclose($output);
	fclose($stream);

	return self::$download_dir.md5($url).'.output';
    }
}

$url = 'http://dlc.sun.com.edgesuite.net/netbeans/7.2.1/final/bundles/netbeans-7.2.1-ml-php-linux.sh';

download::loading($url);

?>